package org.test.Disance;

import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.test.PointeDef;

/**
 * 使用 geotools 类库，求两个点的距离
 */
public class ByGeotools {
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 84坐标系构造GeodeticCalculator
        GeodeticCalculator geodeticCalculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
        // 起点经纬度
        geodeticCalculator.setStartingGeographicPoint(longitude1, latitude1);
        // 末点经纬度
        geodeticCalculator.setDestinationGeographicPoint(longitude2, latitude2);

        // 计算距离，单位：米
        return geodeticCalculator.getOrthodromicDistance();
    }

    public static void main(String[] args) {
        double distance = ByGeotools.getDistance(PointeDef.beijing.getLongitude(), PointeDef.beijing.getLatitude(),
                PointeDef.guangzhou.getLongitude(), PointeDef.guangzhou.getLatitude());
        System.out.println(distance);
        // 1885847.6861785762
    }
}
